﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace asp.net_mvc_上传和下载.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
        public ActionResult UpLoad() {


            return View();
        }
        public ActionResult MyDownLoad() {

            return View();
        }
        //上传处理程序
        public ActionResult File()
        {
            HttpPostedFileBase file = Request.Files["document[file]"];
            if (file != null)
            {
                string fileName = Request.Form["file_name"];
                //  string fileName= DateTime.Now.ToLongDateString().ToString();
                string filePath = Path.Combine(HttpContext.Server.MapPath("../UpLoads/" + fileName));
                file.SaveAs(filePath);
                ViewBag.fn = fileName;
                // return RedirectToAction("File", "Home");
                return View();

            }
            ViewBag.Message = "Your contact page.";

            return View();
        }
        //下载处理程序
        public ActionResult DownLoad(string fileName)
        {
            //  string absoluFilePath = Server.MapPath(System.Configuration.ConfigurationManager.AppSettings["AttachmentPath"] + filePath);
            if (fileName != null)
            {
                string filePath = Path.Combine(HttpContext.Server.MapPath("../UpLoads/" + fileName));
                return File(new FileStream(filePath, FileMode.Open), "application/octet-stream", Server.UrlEncode(fileName));
            }
            return View();

        }
    }
}